ESP32でHTTP POSTする
これを参考にした(というかほぼそのまま)
code:arduino
/*
* code from here
*
*/
include <WiFi.h>
const char *ssid = "YOUR_SSID"
const char *password = "YOUR_WPA_KEY"
unsigned long lastTime = 0;
unsigned long timerDelay = 5000;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds");
}
void loop() {
// put your main code here, to run repeatedly:
if ((millis() - lastTime) > timerDelay) {
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
http.addHeader("Content-Type", "application/json");
String httpRequestData = "{\"state\": 0}";
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}